const App = getApp();
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
var moment = require("../../utils/moment.min")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    site:"",
    time:"",
    type:"",
    currentDate: new Date().getTime(),
    timeshow:false,
    active: 0,
    index:0,
    sitelist:[],
    carlist:[],
    siteshow:false,
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      } 
      if (type === 'month') {
        return `${value}月`;
      }
      return value;
    },
   

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

      var type = wx.getStorageSync("type");
      console.log("type",type);
      this.setData({type:type})

      console.log("哈哈")
      var that = this;
      var token = wx.getStorageSync('token');
        App._post_form('wxapi/site/list', {
          token
        }, function(result) {
          
            if(result.status=="200"){
              that.setData({sitelist:result.data})
            }
        });

        var now = moment().format("YYYY-MM-DD");
        console.log("now",now);

        this.setData({time:now})

  
     
        this.getmysite()

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  choosesite:function(e){
    console.log("choosesite",e);
    // var site = this.data.sitelist[e.detail.value].name;

    this.setData({
      index: e.detail.value
    })

  },
  choosetime:function(){
    console.log("???")
    this.setData({timeshow:true})
  },
  onConfirm(event) {
    console.log("ss",event);
    this.setData({
      timeshow: false,
      time: this.formatDate(event.detail),
    });
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getMonth() + 1}/${date.getDate()}`;
  },
  onClose() {
    this.setData({ timeshow: false });
  },
  home(){

  },
  myinfo(){
    console.log("ss");
    wx.redirectTo({
      url: '/pages/user/user'
    })
  },
  onClose() {
    this.setData({ siteshow: false });
  },

  onSelect(event) {
    console.log("site",event.detail);
    this.setData({site:event.detail.name})
  },
  onInput(event) {
    console.log("date!!!!",event.detail);
    //var date = moment(event.detail).format("YYYY-MM-DD");

    this.setData({
      currentDate: event.detail,
    });
  },
  onClosedate() {
    this.setData({ timeshow: false });
  },
  onSelectdate(uitem) {
    console.log("fff")
    this.setData({
      time: moment(uitem.detail).format("YYYY-MM-DD"),
    });
    this.setData({ timeshow: false });
  },
  search(){
    var that = this;
    var datestr = this.data.time;
    var site = this.data.sitelist[this.data.index].name;

    console.log("????",site);

 
    
    var token = wx.getStorageSync('token');
    App._post_form('wxapi/car/list', {
      token:token,datestr:datestr,site:site
    }, function(result) {
      
        if(result.status=="200"){
          if(result.data.length==0){
            Dialog.alert({
              title: '系统提示',
              message: '暂无数据',
            }).then(() => {
              that.setData({carlist:result.data})
            });
          
         
          
          }else{
            that.setData({carlist:result.data})
          }
         
        }else{
         
          Dialog.alert({
            title: '系统提示',
            message: result.message,
          }).then(() => {
           
          });
        
        }
    });
  },
  yuyue(e){
    var that = this;
    var workid = e.currentTarget.dataset['id'];
    var token = wx.getStorageSync('token');
    var site = this.data.sitelist[this.data.index].name;
    App._post_form('wxapi/yuyue', {
      token:token,workid:workid,site:site
    }, function(result) {
      
        if(result.status=="200"){

          Dialog.alert({
            title: '预约成功',
            message: '您可前往个人中心-我的预约查看预约详情及乘车码，祝您乘车愉快！',
          }).then(() => {
            // on close
             that.search();
          });
          
        }else{
          Dialog.alert({
            title: '系统提示',
            message: result.message,
          }).then(() => {
            // on close
          });
        }
    });
  },
  
  gocheck(){
    wx.navigateTo({
      url: '/pages/index/homecheck',
    })
  },
  getmysite(){
    var that = this;
    var token =  wx.getStorageSync('token');
    App._post_form('wxapi/mysite', {
      token:token
    }, function(result) {
      console.log("????",result);
        if(result.status=="200"){
          var num =0;
          var address= result.data;
          var list = that.data.sitelist;
          for(var i=0;i<list.length;i++){
            if(address==list[i].name){
                num=i;
                break;
            }
          }
          that.setData({index:num})
        
          
        }else{
         
        }
    });
  }


})